﻿using System;
using System.Collections.Generic;
using System.Text;
using reactiveBT.Utilities;

namespace reactiveBT.BT
{
    /// <summary>
    /// 固定次数循环分支节点
    /// 到达规定的循环次数前，均返回Continue。单次循环后重置所有子节点
    /// 向上传递Abort信息，循环结束后返回Failure
    /// </summary>
    public class Repeat : Branch
    {
        int count = 1;
        int currentCount = 0;
        public Repeat(int count)
        {
            this.count = count;
        }
        public override BTState Tick(int BTid)
        {
            if (needToReset)
            {
                this.ResetSelf();
                base.ResetChildren();
                needToReset = false;
            }
            if (count > 0 && currentCount < count)
            {
                var result = base.Tick(BTid);
                switch(result)
                {
                    case BTState.Continue:
                        return BTState.Continue;
                    case BTState.Abort:
                        needToReset = true;
                        return BTState.Abort;
                    default:
                        currentCount++;
                        base.ResetSelf();
                        base.ResetChildren();
                        if (currentCount == count)
                        {
                            needToReset = true;
                            return BTState.Success;
                        }
                        return BTState.Continue;
                }
            }
            return BTState.Failure;
        }
        protected override void ResetSelf()
        {
            //理论上可以不用考虑父类，父类会对activeChild做重置
            //base.ResetSelf();
            currentCount = 0;
        }
    }
}
